<?php

class Image {

    public function input() {
        return $this;
    }

    # 验证码
    public function Verify( $width = 110, $height = 35, $imageType = 'png' ) {
        header( "Content-type: image/" . $imageType );

        $c = array( "-", '*', '+' );
        $a = mt_rand( 2, 9 );
        $b = mt_rand( 2, 9 );
        $c = $c[ mt_rand( 0, 2 ) ];
        $d = "=";
        $e = "?";

        $randval = array( $a, $c, $b, $d, $e );

        $v = 0;
        eval( "\$v = $a $c $b;" );
        $_SESSION[ 'verify' ] = $v;

        $length = count( $randval );

        $im = imagecreate( $width, $height );
        imagefill( $im, 0, 0, imagecolorallocate( $im, 255, 255, 255 ) );
        $fontColor[ ] = imagecolorallocate( $im, 0x15, 0x15, 0x15 );
        $fontColor[ ] = imagecolorallocate( $im, 0x95, 0x1e, 0x04 );
        $fontColor[ ] = imagecolorallocate( $im, 0x93, 0x14, 0xa9 );
        $fontColor[ ] = imagecolorallocate( $im, 0x12, 0x81, 0x0a );
        $fontColor[ ] = imagecolorallocate( $im, 0x06, 0x3a, 0xd5 );

        // 画背景
        imagefilledrectangle( $im, 0, 0, $width, $height, imagecolorallocate( $im, 235, 236, 237 ) );
        // 画干扰线
        for ( $i = 0; $i < 20; $i++ ) {
            $font_color = imagecolorallocate( $im, mt_rand( 0, 255 ), mt_rand( 0, 255 ), mt_rand( 0, 255 ) );
            imagearc( $im, mt_rand( -$width, $width ), mt_rand( -$height, $height ), mt_rand( 30, $width * 2 ), mt_rand( 20, $height * 2 ), mt_rand( 0, 360 ), mt_rand( 0, 360 ), $font_color );
        }
        // 画干扰点
        for ( $i = 0; $i < 50; $i++ ) {
            $font_color = imagecolorallocate( $im, mt_rand( 0, 255 ), mt_rand( 0, 255 ), mt_rand( 0, 255 ) );
            imagesetpixel( $im, mt_rand( 0, $width ), mt_rand( 0, $height ), $font_color );
        }

        $font = dirname( dirname( __FILE__ ) ) . '/font/monaco.ttf';
        $fontSize = 24;
        for ( $i = 0; $i < $length; $i++ ) {
            $x = floor( $width / $length ) * $i + 5;
            $y = $height / 2 + $fontSize / 2;
            imagettftext( $im, $fontSize, rand( 0, 5 ), $x, $y, $fontColor[ $i ], $font, $randval[ $i ] );
        }
        $this->output( $im, $imageType );
    }

    private function output( $im, $type ) {
        $ImageFun = 'image' . $type;
        $ImageFun( $im );
        imagedestroy( $im );
    }
}